<template>
  <div class="profile-page">
    <div class="user-info">
      <div class="container">
        <div class="row">
          <div class="col-xs-12 col-md-10 offset-md-1">
            <img :src="profileInfo.image" class="user-img" />
            <h4>{{profileInfo.username}}</h4>
            <p>{{profileInfo.bio}}</p>
            <button
              v-if="!isUser"
              class="btn btn-sm btn-outline-secondary action-btn"
              :class="{
                active: profileInfo.following
              }"
            >
              <i class="ion-plus-round"></i>
              &nbsp; {{profileInfo.following ? 'Unfollow': 'Follow'}} {{profileInfo.username}}
            </button>
          </div>
        </div>
      </div>
    </div>

    <div class="container">
      <div class="row">
        <div class="col-xs-12 col-md-10 offset-md-1">
          <div class="articles-toggle">
            <ul class="nav nav-pills outline-active">
              <li class="nav-item">
                <nuxt-link
                  exact
                  class="nav-link"
                  :class="{
                    active: !this.$route.query.tab
                  }"
                  :to="{
                    path: this.$route.path,
                    query: {
                      page: 1
                    }
                  }"
                >My Articles</nuxt-link>
              </li>
              <li class="nav-item">
                <nuxt-link
                  exact
                  class="nav-link"
                  :class="{
                    active: this.$route.query.tab
                  }"
                  :to="{
                    path: this.$route.path,
                    query: {
                      page: 1,
                      tab: 'favorite'
                    }
                  }"
                >Favorited Articles</nuxt-link>
              </li>
            </ul>
          </div>

          <article-item :articles="articles" />
          <page-item :pageCount="pageCount" />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import { getProfile } from "@/api/profile";
import { getArticles } from "@/api/home";
import ArticleItem from "@/components/article-item";
import PageItem from "@/components/page-item";

export default {
  name: "profile",
  components: {
    ArticleItem,
    PageItem
  },
  data() {
    return {
      profileInfo: {},
      articles: [],
      limit: 5,
      articlesCount: 0
    };
  },
  watch: {
    $route(to, from) {
      this.updateProfile();
    }
  },
  computed: {
    ...mapState(["user"]),
    pageCount() {
      return Math.ceil(this.articlesCount / this.limit);
    },
    isUser() {
      return this.user && this.$route.params.username === this.user.username;
    }
  },
  async created() {
    // if (this.user && this.user.username === this.$route.params.username) {
    //   this.profileInfo = this.user;
    //   return;
    // }
    this.updateProfile();
  },
  methods: {
    async updateProfile() {
      const username = this.$route.params.username;
      const isFavorite = this.$route.query.tab === "favorite";
      const [profile, articles] = await Promise.all([
        getProfile(username),
        getArticles({
          author: isFavorite ? undefined : username,
          favorited: isFavorite ? username : undefined,
          limit: this.limit,
          offset: (parseInt(this.$route.query.page) - 1 || 0) * this.limit
        })
      ]);
      this.articles = articles.data.articles;
      this.articlesCount = articles.data.articlesCount;
      this.profileInfo = profile.data.profile;
    }
  }
};
</script>